
#include <BGeometry>

#include "EdgeTools.h"

bool CreateEdgeNeighborMap(const BGeometry* geometry, BElement* element, EdgeNeighborMap& edgeNeighborMap)
{
	//CellHolderArray* cells = new CellHolderArray();
	//element->setAsset(cells);
	edgeNeighborMap.reset();
	if (geometry && element)
	{
		const BVectorArray* vertices = geometry->vertices();
		if (const BIntArray* indices = element->indices())
		{
			if (element->primitive() == Primitive_Triangles)
			{
				//cells->reserve(indices->size() / 3);
				for (int i = 0; i < indices->size(); i += 3)
				{
					int ia = indices->at(i);
					int ib = indices->at(i + 1);
					int ic = indices->at(i + 2);
					const BVector& va = vertices->at(ia);
					const BVector& vb = vertices->at(ib);
					const BVector& vc = vertices->at(ic);
					TrigCell* cell = new TrigCell(ia, ib, ic, va, vb, vc);
					edgeNeighborMap[Edge(ia, ib, va, vb)].addCell(cell);
					edgeNeighborMap[Edge(ib, ic, vb, vc)].addCell(cell);
					edgeNeighborMap[Edge(ic, ia, vc, va)].addCell(cell);
					//cells->append(cell);
				}
			}
			if (element->primitive() == Primitive_Quads)
			{
				//cells->reserve(indices->size() / 4);
				for (int i = 0; i < indices->size(); i += 4)
				{
					int ia = indices->at(i);
					int ib = indices->at(i + 1);
					int ic = indices->at(i + 2);
					int id = indices->at(i + 3);
					const BVector& va = vertices->at(ia);
					const BVector& vb = vertices->at(ib);
					const BVector& vc = vertices->at(ic);
					const BVector& vd = vertices->at(id);
					QuadCell* cell = new QuadCell(ia, ib, ic, id, va, vb, vc, vd);
					edgeNeighborMap[Edge(ia, ib, va, vb)].addCell(cell);
					edgeNeighborMap[Edge(ib, ic, vb, vc)].addCell(cell);
					edgeNeighborMap[Edge(ic, id, vc, vd)].addCell(cell);
					edgeNeighborMap[Edge(id, ia, vd, va)].addCell(cell);
					//cells->append(cell);
				}
			}
			return true;
		}
		if (const BPieceArray* pieces = element->pieces())
		{
			if (element->primitive() == Primitive_Triangles)
			{
				//cells->reserve(pieces->size() / 3);
				for (int i = 0; i < pieces->size(); i += 3)
				{
					int ia = pieces->at(i + 0).v();
					int ib = pieces->at(i + 1).v();
					int ic = pieces->at(i + 2).v();
					const BVector& va = vertices->at(ia);
					const BVector& vb = vertices->at(ib);
					const BVector& vc = vertices->at(ic);
					TrigCell* cell = new TrigCell(ia, ib, ic, va, vb, vc);
					edgeNeighborMap[Edge(ia, ib, va, vb)].addCell(cell);
					edgeNeighborMap[Edge(ib, ic, vb, vc)].addCell(cell);
					edgeNeighborMap[Edge(ic, ia, vc, va)].addCell(cell);
					//cells->append(cell);
				}
			}
			if (element->primitive() == Primitive_Quads)
			{
				//cells->reserve(pieces->size() / 4);
				for (int i = 0; i < pieces->size(); i += 4)
				{
					int ia = pieces->at(i + 0).v();
					int ib = pieces->at(i + 1).v();
					int ic = pieces->at(i + 2).v();
					int id = pieces->at(i + 3).v();
					const BVector& va = vertices->at(ia);
					const BVector& vb = vertices->at(ib);
					const BVector& vc = vertices->at(ic);
					const BVector& vd = vertices->at(id);
					QuadCell* cell = new QuadCell(ia, ib, ic, id, va, vb, vc, vd);
					edgeNeighborMap[Edge(ia, ib, va, vb)].addCell(cell);
					edgeNeighborMap[Edge(ib, ic, vb, vc)].addCell(cell);
					edgeNeighborMap[Edge(ic, id, vc, vd)].addCell(cell);
					edgeNeighborMap[Edge(id, ia, vd, va)].addCell(cell);
					//cells->append(cell);
				}
			}
			return true;
		}
		int first = element->first();
		int count = element->count();
	}
	return false;
}
